package com.servlet;

import com.alibaba.fastjson.JSON;

import com.bean.Announce;
import com.bean.AnnouncePage;
import com.dao.AnnounceDao;
import com.utils.ResponseUtils;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName AnnounceServlet
 * @Desc 前台公告控制层
 * @Author xiaoding
 * @Date 2021/1/17 20:54
 * @Version 1.0
 */
@WebServlet(name = "AnnounceServlet",value = "/AnnounceServlet")
public class AnnounceServlet extends HttpServlet {
    private AnnounceDao announceDao = new AnnounceDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //获取公告信息的方法
            List<Announce> list = announceDao.announceSelects();

            //将数据转换为json格式
            JSONObject obj = new JSONObject();
            //添加数据
            obj.put("data", JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd hh:mm:ss"));
            //发送数据
            ResponseUtils.writeHTML(response,obj.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
